define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            var ids = Fast.api.query('ids')

            Table.api.init({
                extend: {
                    index_url: `subject/subject/index?ids=${ids}`, //列表查询的请求控制器方法
                    add_url: 'subject/subject/add', //添加的控制器地址
                    edit_url: 'subject/subject/edit', //编辑的控制器地址
                    del_url: 'subject/subject/del', //删除的控制器地址
                    info_url: 'subject/info/index', //课程详情地址
                    chapter_url: `subject/chapter/index?ids=${ids}`, //章节详情地址
                    table: 'subject',
                }
            });

            //获取view视图里面的dom元素table元素
            var table = $("#table")
            
            //渲染列表数据
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url, //请求地址
                toolbar: ".toolbar", //工具栏
                pk: 'id', //默认主键字段名
                sortName: 'createtime', //排序的字段名
                sortOrder: 'desc', //排序的方式
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [ //渲染的字段部分
                    {checkbox: true},
                    {field: 'title', title: __('Title'), operate: 'LIKE',sortable:true},
                    {field: 'category.name', title: __('Category'),searchList:Config.categoryList},
                    {
                        field: 'thumbs', 
                        title: __('Thumbs'), 
                        operate: false,
                        events: Table.api.events.image,
                        formatter: Table.api.formatter.image
                    },
                    {field: 'price', title: __('Price'),operate: false, sortable:true},
                    {field: 'likes_num', title: __('Likes'),operate: false},
                    {
                        field: 'createtime', 
                        title: __('CreateTime'), 
                        operate: 'RANGE', 
                        addclass: 'datetimerange', 
                        sortable:true,
                        formatter: Table.api.formatter.datetime
                    },
                    //最后一排的操作按钮组
                    {
                        field: "operate", 
                        title: __('Operate'),
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                                name: 'info',
                                title: function(data)
                                {
                                    return `${data.title}-课程详情`
                                },
                                icon: 'fa fa-ellipsis-h',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: $.fn.bootstrapTable.defaults.extend.info_url,
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                            },
                            {
                                name: 'chapter',
                                title: function(data)
                                {
                                    return `${data.title}-章节详情`
                                },
                                icon: 'fa fa-align-justify',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: $.fn.bootstrapTable.defaults.extend.chapter_url,
                                extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                            }
                        ]
                    }
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'subject/subject/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'subject/subject/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'subject/subject/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
